#!/usr/env/bin python
# -*- coding: utf-8 -*-

# @Time    : 2018/11/13 17:53
# @Author  : yangdy
# @Email   : yangdy@egu360.com
# @File    : 中级-x的平方根.py
# @Software: PyCharm


class Solution:
    def mySqrt(self, x):
        """
        :type x: int
        :rtype: int
        """
        # import math
        # return int(math.sqrt(x))
        # if x == 1:
        #     return x
        l = 0
        r = x * 2
        while r - l > 0.0001:
            m = (l + r) / 2
            mm = int(m) ** 2
            print(l, r, m, mm)
            if mm > x:
                r = m
            elif mm < x:
                l = m
            else:
                return int(m)
        return int(l)


def opt(x):
    solve = Solution()
    print('x:', x)
    print('ans:', solve.mySqrt(x))


def main():

    opt(4)

    opt(8)

    opt(1)

    opt(9)

    opt(2147395600)


if __name__ == '__main__':
    main()
